/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.struts2.action;

import com.aptech.business.NewsManager;
import com.aptech.domain.JPA.Category;
import com.aptech.business.INewsManager;
import com.aptech.domain.JPA.News;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.List;
import org.apache.log4j.Logger;
/**
 *
 * @author Admin
 */
public class NewsCRUDAction extends ActionSupport implements ModelDriven<NewsCRUDActionModel> {
    static final Logger logger = Logger.getLogger(NewsCRUDAction.class);
    private NewsCRUDActionModel model = new NewsCRUDActionModel();
    private INewsManager newsManager;

    public INewsManager getNewsManager() {
        return newsManager;
    }

    public void setNewsManager(INewsManager newsManager) {
        this.newsManager = newsManager;
    }

    public NewsCRUDActionModel getModel() {
        return model;
    }
    public String executeListAllNews() {
        try {
            List<News> newsList = newsManager.getAllNews();
            model.setNewsList(newsList);
        } catch (Exception e) {
            logger.debug(e);
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeNewsDetail() {
        try {
            if (model.getBaseSearchCondition() !=null) {
                News news = newsManager.getNewsDetail(model.getBaseSearchCondition());
                System.out.println(news);
                model.setNews(news);
            }
        } catch (Exception e) {
            logger.debug(e);
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeListCategory() {
        try {
            List<Category> categoryList = newsManager.getAllCategory();
            model.setCategoryList(categoryList);
        } catch (Exception e) {
            logger.debug(e);
            return ERROR;
        }
        return SUCCESS;
    }
    public String executeViewCategory() {
        try {
            if(model.getBaseSearchCondition() != null) {
                List<News> newsList = newsManager.getNewsForCategory(model.getBaseSearchCondition());
                model.setNewsList(newsList);
            }
        } catch (Exception e) {
            logger.debug(e);
            return ERROR;
        }
        return SUCCESS;
    }

}
